package arraylist;

import java.util.ArrayList;
import java.util.Objects;

public class Test02 {
    public static void main(String[] args) {
        //创建ArrayList集合对象
        ArrayList< Student> list = new ArrayList<Student>();
        Student s1 = new Student("1","张三","18");
        Student s2 = new Student("2","李四","19");
        Student s3 = new Student("3","王五","20");
        //添加数据
        list.add(s1);
        list.add(s2);
        list.add(s3);
        //遍历
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}
//定义学生类
class Student{
    //属性
    private String id;//学号
    private String name;//姓名
    private String age;//年龄
    //有参构造
    public Student(String id, String name, String age){
        this.id=id;
        this.name=name;
        this.age=age;
    }
    //无参构造
    public Student(){}
    //get and set
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
    //重写equals方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id && age == student.age && Objects.equals(name, student.name);
    }
    //重写hashCode方法
    @Override
    public int hashCode() {
        return Objects.hash(id, name, age);
    }
    //重写toString方法
    @Override
    public String toString() {
        return "学生{" +
                "学号：" + id +
                ", 姓名：'" + name + '\'' +
                ", 年龄：" + age +
                '}';
    }
}
